{extend name="base" /}
{block name="main"}

<div class="fly-panel fly-panel-user">
    <div class="tpt—admin">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加图片</legend>
        </fieldset>
        <form class="layui-form">

            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" required lay-verify="required" placeholder="请输入内容"
                           autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">图片</label>
                <a class="layui-btn" id="image" style="float: left;"><i class="layui-icon">&#xe67c;</i>上传图片</a>
                <div class="layui-input-block" style="margin-left: 220px;">
                    <input type="text" name="pic" class="layui-input" style="position: absolute;left: 0;top: 0;">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">所属类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="type" value="1" title="首页幻灯" checked="">
                    <input type="radio" name="type" value="2" title="其他图片">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">连接</label>
                <div class="layui-input-block">
                    <input type="text" name="links" placeholder="请输入内容" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="banner_add">立即提交</button>
                    <button class="layui-btn layui-btn-primary" onclick="history.go(-1)">返回</button>
                </div>
            </div>

        </form>
        <script>
            layui.use(['form', 'upload'], function () {
                var form = layui.form,
                    upload = layui.upload,
                    jq = layui.jquery;

                upload.render({
                    url: '{:url("upload/upimage")}'
                    , elem: '#image'
                    , before: function (input) {
                        loading = layer.load(2, {
                            shade: [0.2, '#000']
                        });
                    }
                    , done: function (res) {
                        layer.close(loading);
                        jq('input[name=pic]').val(res.path);
                        layer.msg(res.msg, {icon: 1, time: 1000});
                    }
                });

                form.on('submit(banner_add)', function (data) {
                    loading = layer.load(2, {
                        shade: [0.2, '#000']
                    });
                    var param = data.field;
                    jq.post('{:url("banner/add")}', param, function (data) {
                        if (data.code == 200) {
                            layer.close(loading);
                            layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                                location.href = '{:url("banner/index")}';
                            });
                        } else {
                            layer.close(loading);
                            layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
                        }
                    });
                    return false;
                });

            })
        </script>
    </div>
</div>
{/block}